﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data.SqlTypes;

namespace DigDes.DS2012.BaseLoader
{
    public abstract class BaseParser
    {
        protected internal static DateTime ConvertDate(string str, int length, int year, out string newStr)
        {
            DateTime tempDate = new DateTime();
            newStr = str.Substring(length);
            str = str.Substring(0, length);

            if (!DateTime.TryParseExact(String.Format("{0}{1}{2}", str.Substring(0, 4), year, str.Substring(4, 4)),
                "ddMMyyyyHHmm", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate))
                return new DateTime(1754, 1, 1, 1, 1, 1, 1);

            if(tempDate < SqlDateTime.MinValue.Value)
                return new DateTime(1754, 1, 1, 1, 1, 1, 1);
 
            return tempDate.ToUniversalTime();
        }

        protected internal static DateTime ConvertDate(string str, int length, out string newStr)
        {
            DateTime tempDate = new DateTime();
            newStr = str.Substring(length);
            str = str.Substring(0, length);

            if (!DateTime.TryParseExact(str, "ddMMyyyyHHmm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate))
                return new DateTime(1754, 1, 1, 1, 1, 1, 1);

            if (tempDate < SqlDateTime.MinValue.Value)
                return new DateTime(1754, 1, 1, 1, 1, 1, 1);

            return tempDate.ToUniversalTime();
        }

        protected internal static int ConvertInt32(string str, int length, out string newStr)
        {
            var bufInt32 = 0;
            newStr = str.Substring(length);
            str = str.Substring(0, length);

            if (!Int32.TryParse(str, out bufInt32))
                return -1;

            return bufInt32;
        }

        protected internal static string SubString(string str, int length, out string newStr)
        {
            newStr = str.Substring(length);

            return str.Substring(0, length);
        }
    }
}
